import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Bank {
//this is me you looking for
    Scanner input = new Scanner(System.in);
    ReadWriter read = new ReadWriter();
//    static BankAccount[] accounts = new BankAccount[10];
    ArrayList<BankAccount> accounts = new ArrayList<BankAccount>();
//     ArrayList<TransferHistory> th= new ArrayList<TransferHistory>();
    //Bank simple = new Bank();
    String name;
    int accountNr;
    int amount;
    boolean stop = false;

    public static void main(String[] args) {

        Bank bank = new Bank();

    }
   

    public Bank() {
        accounts = read.readFromFile();
        simpleMessage();
        myHope();
    }

    private void myHope() {
        
        while (!stop) {
            try {
                int option = input.nextInt();
                switch (option) {
                    case 1:

                        option1();

                        break;
                    case 2:
                        option2();
                        break;
                    case 3:
                        option3();
                        break;

                    case 4:
                        option4();
                        break;
                    case 5:
                       
                        option5();
                        break;
                    case 6:
                         read.writeToFile(accounts);
                        option6();
                        break;
                        
                    case 7:
                       
                        break;
                    default:
                        System.out.println("You have the wrong number\n  Please try to follow  the instruction!");
                        System.out.println("****************************************");
                        simpleMessage();

                }
            } catch (InputMismatchException e) {
                input.nextLine();
                System.out.println("You have been entering  letter(s) instead of Number(s)\nPlease follow the instruction!");
                System.out.println("********************************************");
                simpleMessage();
            }

        }
    }

    private void option1() {
      
        System.out.println("You have chosen to create a new account");
        System.out.println("Enter your name: ");
            try{
               
                
        name = input.next();
        
        System.out.println(" Enter the initiating amount : ");
       
            amount = input.nextInt();
          
       int lastIndex = accounts.size() - 1;

        if (lastIndex != -1) {
            int a = accounts.get(lastIndex).getAccountNr();
            accountNr = a + 1;
        } else {
            accountNr = 1;
        }
        accounts.add(new BankAccount(amount, accountNr, name));
        simpleMessage();
    }catch(Exception e){
               System.out.println("you have enterning wrong value");
    }
    }

    private void option2() {
        int inAmount = 0;
       
            for (int i = 0; i < accounts.size(); i++) {
                System.out.println(" Account number: " + accounts.get(i).getAccountNr() + "  Name :  " + accounts.get(i).getName() + "  Amount:  " + accounts.get(i).getAmount());
            }
        System.out.println("To transfer from/to external banks press 1");
        System.out.println("To transfer within the bank accounts press 2");
        
        int ad = input.nextInt();
        if (ad == 1 ) {
            System.out.println("Select the account number you want to withdraw ");
            try{
            int aa = input.nextInt();
            
            for (int i = 0; i < accounts.size(); i++) {
                if (aa == accounts.get(i).getAccountNr()) {
                    System.out.println("How much do you want to withdraw? ");
                    inAmount = input.nextInt();
                  
                    amount = accounts.get(i).getAmount() - inAmount;
                        
                     accounts.get(i).setAmount(amount);
                }
                 
            }
            }catch(InputMismatchException e){
                System.out.println("blblblbllblb");
            }
            System.out.println("Select the account Number you want to deposite:");
            int bb = input.nextInt();
            for (int i = 0; i < accounts.size(); i++) {

                if (bb == accounts.get(i).getAccountNr()) {
                    amount = accounts.get(i).getAmount() + inAmount;
                    accounts.get(i).setAmount(amount);
                }
            }
        }
        else if (ad == 2) {
            System.out.println("You have chosen to deposit or withdraw");
            System.out.println("Select the account you want to deposit/withdraw");
            
            int cc = input.nextInt();
            
            for (int i = 0; i < accounts.size(); i++) {
                if (cc == accounts.get(i).getAccountNr()) {
                    System.out.println("Enter negativ number if you want withdraw");
                    System.out.println("Enter positiv number if you want deposit: ");
                    inAmount = input.nextInt();
                    amount = amount + inAmount;
                    amount = accounts.get(i).getAmount() + inAmount;
                    accounts.get(i).setAmount(amount);
                }
                }
            }else {
            System.out.println("you have been entered the wrong value!!");
        }
    
        simpleMessage();
    }
    
    private void option3() {

        delet();
        simpleMessage();
    }

    private void option4() {
        try {
            System.out.println("Write the account number you want to print:");
            accountNr = input.nextInt();
        } catch (InputMismatchException ex) {
            System.out.println("option 4");
        }
        for (int i = 0; i < accounts.size(); i++) {
            if (accountNr == accounts.get(i).getAccountNr()) {
                System.out.println("Account Number:" + accounts.get(i).getAccountNr() + "  Name " + accounts.get(i).getName() + " Amount:  " + accounts.get(i).getAmount());
            }
        }
        System.out.println("you have chose wrong number");// something wrong here you need to look at it
        simpleMessage();
    }
    private void option5() {
        for (int i = 0; i < accounts.size(); i++) {
            System.out.println(" Account number: " + accounts.get(i).getAccountNr() + "  Name :  " + accounts.get(i).getName() + "  Amount:  " + accounts.get(i).getAmount());
        }
        simpleMessage();
    }

    private void option6() {
        System.out.println("Thanks for visiting our Bank");
        stop = true;
    }

    private void simpleMessage() {
        System.out.println("Welcome to my bank system");
        System.out.println("Please follow the Main Menu");
        System.out.println("Main Menu");
        System.out.println("1. Create a new account");
        System.out.println("2. Deposit/Withdraw");
        System.out.println("3. Delete an account");
        System.out.println("4.View one account");
        System.out.println("5.View all the accounts");
        System.out.println("6. Quite");
        System.out.println("7. Transfer History ");
    }
    
    public void delet() {
        
        try{
            
        for (int i = 0; i < accounts.size(); i++) {
             
            System.out.println("Account Number:" + accounts.get(i).getAccountNr() + "  Name " + accounts.get(i).getName() + " Amount:  " + accounts.get(i).getAmount());
        }
        System.out.println("Which account do you wanna to delet?");
        int ac = input.nextInt();
        for (int i = 0; i < accounts.size(); i++) {
            if (ac == accounts.get(i).getAccountNr()) {
                
                
                System.out.print("acount Number  " + accounts.get(i).getAccountNr());
                accounts.remove(i);
                
                System.out.println("  is deleted");
            
            }else {
                System.out.println("Account Number is not found");
                
            }
        }
        }catch(Exception e){
            System.out.println("The delet");
        }
    }
}